package com.miao.shop.controller;

import com.miao.common.entity.R;
import com.miao.shop.entity.ShopCar;
import com.miao.shop.service.ShopCarService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author MiaoWenqing
 * @description: 购物车
 * @date 2021/2/17 11:30
 */
@RestController
@RequestMapping("/common/car")
public class ShopCarController {


    @Resource
    private ShopCarService shopCarService;

    /**
     * 添加商品
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return R
     */
    @GetMapping("/add")
    public R addProduct(String pId, String sId, String uId, int num) {
        if (this.shopCarService.addProduct(pId, sId, uId, num)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     * 删除商品
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return R
     */
    @GetMapping("/del")
    public R delProduct(String pId, String sId, String uId) {
        if (this.shopCarService.delProduct(pId, sId, uId)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     * 修改商品数量
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @param num 数量
     * @return R
     */
    @GetMapping("/num")
    public R editNum(String pId, String sId, String uId, int num) {
        if (this.shopCarService.editNum(pId, sId, uId, num)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     * 修改选中状态
     *
     * @param pId 商品id
     * @param sId 店铺id
     * @param uId 用户id
     * @return R
     */
    @GetMapping("/sel")
    public R editSelect(String pId, String sId, String uId) {
        if (this.shopCarService.editSelect(pId, sId, uId)) {
            return R.ok();
        }
        return R.error();

    }

    /**
     * 根据用户id 获取购物车
     * @param uId 用户id
     * @return list
     */
    @GetMapping("/get")
   public R getCar(String uId){
        return R.ok(this.shopCarService.getCar(uId));
   }
}
